package com.tommy.ddp.manager
{
	import com.tommy.ddp.GridDataManager;
	import com.tommy.ddp.MainManager;
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.AxisConfig;
	import com.tommy.ddp.definition.BoxDisposeAnimationTime;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.disposeHandler.special.ISpecialDisposeHandler;
	import com.tommy.ddp.events.FinderEvent;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.swapHandle.AdvSwapHandler;
	
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import starling.events.Event;

	/**
	 * 棋子交换、计算是否可消除的处理
	 * @author hongxuan
	 * 
	 */	
	public class SwapManager
	{
		//private static var targetPoint_1:Point;
		private static var targetPoint_2:Point;
		private static var _orgBox:DdpBox;
		private static var _newBox:DdpBox;
		private static var _dir:int;
		private static var _axis:int;
		/**
		 * 是否是交换无果以后返回的
		 */		
		private static var _isBack:Boolean = false;
		
		private static var swapHandler:AdvSwapHandler;
		
		public function SwapManager()
		{
		}
		public static function get orgBox():DdpBox
		{
			return _orgBox;
		}
		public static function get newBox():DdpBox
		{
			return _newBox;
		}
		/**
		 * 交换棋子
		 * @param orgBox 起始棋子
		 * @param axis 坐标
		 * @param dir 方向
		 * 
		 */		
		public static function swapBox(orgBox:DdpBox,axis:uint,dir:int):void
		{
			_dir = dir;
			_axis = axis;
			
			var newBox:DdpBox;
			var newGrid:DdpGrid;
			if(axis == AxisConfig.X_AXIS)
			{
				newBox = GridDataManager.getBox(orgBox.column+dir,orgBox.row);
				newGrid = GridDataManager.getGrid(orgBox.column+dir,orgBox.row);
			}
			else
			{
				newBox = GridDataManager.getBox(orgBox.column,orgBox.row+dir);
				newGrid = GridDataManager.getGrid(orgBox.column,orgBox.row+dir);
			}
			
			if(newBox)
			{
				if(newGrid.isAvaliable)
					_swapBox(orgBox,newBox);
			}
		}
		
		private static function _swapBox(orgBox:DdpBox,newBox:DdpBox,isBack:Boolean = false):void
		{
			_isBack = isBack;
			
			_orgBox = orgBox;
			_newBox = newBox;
			/*orgBox.scaleX = 1.5;
			newBox.scaleX = 1.5;*/
			//targetPoint_1 = new Point(orgBox.x,orgBox.y);
			targetPoint_2 = new Point(newBox.x,newBox.y);
			
			orgBox.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			MainManager.game.container.mouseEnabled = false;
		}
		
		private static function onEnterFrame(event:Event):void
		{
			if(_axis == AxisConfig.X_AXIS)
			{
				_orgBox.x += DDPConfig.SWAP_SPEED*_dir;
				_newBox.x += DDPConfig.SWAP_SPEED*-_dir;
				if(_orgBox.x == targetPoint_2.x)
				{
					_orgBox.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
					//trace("step 1 --> swap box");
					changeBoxIndex(_orgBox);
					changeBoxIndex(_newBox);
					//
					moveOver(_orgBox,_newBox);
				}
			}
			else
			{
				_orgBox.y += DDPConfig.SWAP_SPEED*_dir;
				_newBox.y += DDPConfig.SWAP_SPEED*-_dir;
				if(_orgBox.y == targetPoint_2.y)
				{
					_orgBox.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
					//trace("step 1 --> swap box");
					changeBoxIndex(_orgBox);
					changeBoxIndex(_newBox);
					//
					moveOver(_orgBox,_newBox);
				}
			}
		}
		
		/**
		 * 棋子变化后，重新设置列、行信息，并更新HASHMAP里的索引
		 * @param box
		 * 
		 */		
		private static function changeBoxIndex(box:DdpBox):void
		{
			box.column = box.x/DDPConfig.GRID_SIZE_W;
			box.row = box.y/DDPConfig.GRID_SIZE_H;
			
			GridDataManager.regBox(box.x/DDPConfig.GRID_SIZE_W,box.y/DDPConfig.GRID_SIZE_H,box);
		}
		/**
		 * 交换结束以后的处理
		 * @param orgBox
		 * @param newBox
		 * 
		 */		
		private static function moveOver(orgBox:DdpBox,newBox:DdpBox):void
		{
			var vect:Vector.<DdpBox> = new Vector.<DdpBox>();
			vect.push(_orgBox,_newBox);
			
			//判断是不是特殊糖果之间的交换
			var str:String;
			if(orgBox.exeType > newBox.exeType)
				str = orgBox.exeType + "_" + newBox.exeType;
			else
				str = newBox.exeType + "_" + orgBox.exeType;
			var handler:ISpecialDisposeHandler = SpecialDisposeManager.getIsSpecial(str);
			if(handler)
			{
				var time:Number = BoxDisposeAnimationTime.getSpecialTime(str);
				handler.dispose(vect,time);
				return;
			}
				
			//
			if(_isBack)
			{
				_orgBox = null;
				_newBox = null;
				//
				MainManager.game.container.mouseEnabled = true;
				return;
			}
			if(swapHandler)
			{
				swapHandler.dispose();
				swapHandler.removeEventListener(FinderEvent.GET_DISPOSE,onGetDispose);
				swapHandler.removeEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
			}
			swapHandler = new AdvSwapHandler();
			swapHandler.addEventListener(FinderEvent.GET_DISPOSE,onGetDispose);
			swapHandler.addEventListener(FinderEvent.NONE_TO_DISPOSE,onNoneToDispose);
			swapHandler.execute();
		}
		/**
		 * 每次查找完得把org new的引空置空，不然后面查找到交叉判断不出是否由人工操作带的
		 * @param event
		 * 
		 */		
		private static function onNoneToDispose(event:FinderEvent):void
		{
			//在交换行为产生的查找中，没有就将两个BOX退回原位置，退回完毕后不会进行再查找，所以退回完以后再把相关清空
			_swapBox(_newBox,_orgBox,true);
		}
		private static function onGetDispose(event:FinderEvent):void
		{
			_orgBox = null;
			_newBox = null;
		}
	}
}
